/**
 * Copyright HCP Technology 
 * www.hcp.vn
 */
package util;

import java.util.List;

import models.CategoryModel;
import models.ContentModel;
import models.FilterContentData;
import models.UserModel;
import models.HomeAdv;
import play.Logger;
import play.cache.Cache;
import play.mvc.Scope.RenderArgs;

public class SimpleCache extends Cache {

	public static final String HOMEADVS = "homeAdvs";
	public static final String LISTCATEGORY = "listCategories";
	public static final String DATA_FILTER = "data_filter";
	
	public static ContentModel getContent(Long id){
		Logger.info("-------dont find info in Cache-----" + id);
		ContentModel content = (ContentModel)Cache.get("content_" + id);
		if( content == null){
			Logger.info("-------dont find info in Cache-----" + id);
			content = ContentModel.findById(id, true);
			Cache.set("content_" + id, content);
		}
		return content;
	}
	
	public static ContentModel getContent(Long id, String alias, String user){
		ContentModel content = (ContentModel)Cache.get("content_" + id);
		if( content == null){
			Logger.info("-------dont find info content in Cache-----" + id);
			content = ContentModel.findById(id);
			if(content != null && content.active){
				Cache.set("content_" + id, content);	
			}
			if(content != null && user != null && !content.active && user.equals(content.user)){
				return content;
			}
		} 
		if(content != null && !content.alias.equals(alias)) {
			return null;
		}
		if(user != null && UserModel.getUserModelByEmail(user).isAdmin && !content.active){
			return content;
		}
		//just user create content see it,if this content is deactive
		if((user == null || !user.equals(content.user)) && !content.active) {
			return null;
		}
		return content;
	}
	
	public static List<CategoryModel> listCategories() {
		
		List<CategoryModel> categories = (List<CategoryModel>)get(LISTCATEGORY);
		if(categories == null) {
			categories = CategoryModel.listCategory();
		}
		if(categories != null && categories.size() != 0) {
			Cache.set(LISTCATEGORY, categories);
		}
		return categories;
	}
	

	public static CategoryModel findCategory(String alias, long id) {
		
		List<CategoryModel> categories = listCategories();
		if(categories == null) {
			return null;
		}
		if(alias != null) {
			for (CategoryModel categoryModel : categories) {
				if(categoryModel.alias.compareTo(alias) == 0) {
					return categoryModel;
				}
			}
		}
		
		if(id != 0) {
			for (CategoryModel categoryModel : categories) {
				if(categoryModel.id  == id) {
					return categoryModel;
				}
			}
		}
		return null;
	}
	
	public static List<ContentModel> listContentModel(String category) {
		List<ContentModel> listContentModelTotals = get("category_" + category, List.class);
		if(listContentModelTotals == null || listContentModelTotals.size() == 0){
			Logger.info("---dont see in cache---");
			listContentModelTotals = ContentModel.findByCategory(category, true);
			
		}
		if(listContentModelTotals != null) {
			set("category_" + category, listContentModelTotals);
		}
		return listContentModelTotals;
	}
	
	public static int totalContentModel(String category) {
		Integer total = Cache.get("total_category_" + category, Integer.class);
		if(total == null){
			Logger.info("---dont see totalContentModel in cache---");
			total = ContentModel.totalCategory(category);
		}
		if(total != null) {
			set("total_category_" + category, total);
		}
		return total;
	}
	
	public static String getCacheDataFilter(){
		String data = Cache.get(DATA_FILTER, String.class);
		if(data == null || data.isEmpty()){
			FilterContentData  filterContentData =  FilterContentData.getDataFilterContent();
			if(filterContentData != null){
				Cache.set(DATA_FILTER, filterContentData.data);	
			}
		}
		Logger.info("---data filter content >> " + data);
		return data;
	}
	
	public static void updateDataFilterContent(String data){
		Cache.delete(DATA_FILTER);
		Cache.set(DATA_FILTER, data);
	}
	
	public static List<HomeAdv> listHomeAdv() {
		List<HomeAdv> rs = get(HOMEADVS, List.class);
		if(rs == null || rs.size() == 0){
			Logger.info("---dont see in cache---");
			rs = HomeAdv.find(true);
		}
		if(rs != null) {
			set(HOMEADVS, rs);
		}
		return rs;
	}
}
